<template>
  <div>
    <div class="view-login">
      <div class="logo-box">
        <el-image :src="require('../../../static/logo/logo_blue.png')">
          <div slot="placeholder" class="image-slot">
            加载中<span class="dot">...</span>
          </div>
        </el-image>
      </div>
      <div class="login-content">
        <div class="left" style="z-index: 0">
          <el-image :src="require('../../../static/login/newbg.png')">
            <div slot="placeholder" class="image-slot">
              加载中<span class="dot">...</span>
            </div>
          </el-image>
        </div>
        <div class="right" style="z-index: 1">
          <div class="right-content">
            <h2 class="title">
              <span>账号登录</span>
            </h2>
            <ul class="tabs-nav">
              <li v-for="(item, index) in tabsNav" class="item" :class="{active: currentIndex === index}" @click="tabChange(index)">{{item.title}}</li>
            </ul>
            <div class="login-tab" v-if="currentIndex === 0">
              <el-form
                ref="loginForm"
                :model="loginForm"
                :rules="rules"
                label-width="0"
                style="background-color: #fff">
                <el-form-item prop="mobile">
                  <el-input v-model="loginForm.mobile" placeholder="请输入邮箱/手机号/账号" clearable></el-input>
                </el-form-item>
                <el-form-item prop="password">
                  <el-input type="password" v-model="loginForm.password" autocomplete="off"
                            placeholder="请输入密码" show-password></el-input>
                </el-form-item>
                <el-form-item>
                  <div class="flex-between">
                    <el-checkbox v-model="remember">下次自动登录</el-checkbox>
                    <el-button type="text" style="color: #74787c">忘记密码？</el-button>
                  </div>
                </el-form-item>
              </el-form>
              <div class="bottom-box">
                <div style="margin-top: 12px">
                  <el-button type="primary" style="width: 100%" @click="submitLogin('loginForm')">登录</el-button>
                </div>
                <div class="go-register">
                  <p>
                    还没有账号？
                    <span class="font-color-main" @click="goRegister('loginForm')">去注册</span>
                  </p>
                </div>
              </div>
              <div class="qr-box">
                <div class="qr-code"></div>
              </div>
            </div>
            <div class="login-tab"
                 v-if="currentIndex === 1">
              <el-form
                ref="loginForm"
                :model="loginForm"
                :rules="rules"
                label-width="0"
                style="background-color: #fff">
                <el-form-item prop="mobile">
                  <el-input v-model="loginForm.mobile" placeholder="请输入手机号" clearable></el-input>
                </el-form-item>
                <el-form-item prop="verificationCode">
                  <el-input v-model="verificationCode" placeholder="请输入验证码" clearable style="width: 313px"></el-input>
                  <el-button type="primary">发送验证码</el-button>
                </el-form-item>
                <el-form-item>
                  <div class="flex-between">
                    <el-checkbox v-model="remember">下次自动登录</el-checkbox>
                    <el-button type="text" style="color: #74787c">忘记密码？</el-button>
                  </div>
                </el-form-item>
              </el-form>
              <div class="bottom-box">
                <div style="margin-top: 12px">
                  <el-button type="primary" style="width: 100%" @click="submitLogin('loginForm')">登录</el-button>
                </div>
                <div class="go-register">
                  <p>
                    还没有账号？
                    <span class="font-color-main" @click="goRegister('loginForm')">去注册</span>
                  </p>
                </div>
              </div>
              <div class="qr-box">
                <div class="qr-code"></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import crypto from "crypto";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        account: '',
        email: '',
        mobile: '',
        //任选一个传到后端
        password: ''
      },
      remember: false,
      currentIndex: 0,
      verificationCode: '',
      tabsNav: [
        {
          title: '账号登录'
        },
        {
          title: '手机短信登录'
        },
        {
          title: '微信登录'
        },
      ],
      rules: {
        mobile: [
          {required: true, message: '请输入邮箱/手机号/账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    tabChange(index) {
      this.currentIndex = index;
    },
    submitLogin(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          // md5 加密
          let temp = {...this.loginForm};
          let md5 = crypto.createHash("md5");
          md5.update(temp.password);
          temp.password = md5.digest('hex');

          this.$store.dispatch('submitLogin', temp).then(res => {
            if (res.code === 200) {
              this.resetForm(form)
              this.$router.push('/courseList')
            } else {
              this.$router.push('/login')
            }
          })
        } else {
          return false;
        }
      });
    },
    goRegister(form) {
      this.$router.push("/register");
      this.resetForm(form)
    },
    resetForm(form) {
      this.$refs[form].resetFields();
    }
  }
}
</script>

<style scoped>
.view-login {
  position: relative;
  width: 100%;
  min-height: 100vh;
  background: #f8f9fa;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.view-login .el-button, .view-login .el-input__inner {
  height: 48px;
  padding-top: 4px;
  padding-bottom: 4px;
}

.view-regist {
  display: flex;
  flex-direction: column;
  background-size: 100%;
  background-repeat: no-repeat;
  /*padding-top: 20px;*/
}

.view-login .logo-box {
  position: absolute;
  top: 48px;
  left: 62px;
  cursor: pointer;
}

.view-regist .login-content {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.view-login .login-content {
  display: flex;
  justify-content: space-between;
}

.view-login .login-content .left {
  margin-right: -100px;
}

.view-login .login-content .left img {
  display: block;
  height: 538px;
}

.view-login .login-content .right .right-content {
  width: 431px;
  padding: 0 35px;
  background: #fff;
  border: 1px solid #eee;
  box-shadow: 0 0 61px 0 rgb(85 108 144 / 7%);
}

.view-login .login-content .right .title {
  position: relative;
  padding: 18px 0;
  text-align: center;
}

.view-login .login-content .right .title span {
  font-size: 22px;
  font-weight: 700;
}

.view-login .login-content .right .right-content .tabs-nav {
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
}

.view-login .login-content .right .right-content .tabs-nav .item {
  cursor: pointer;
  position: relative;
  padding-bottom: 9px;
  font-size: 18px;
  font-family: PingFangSC, PingFangSC-Regular;
  font-weight: 400;
  text-align: left;
  color: #74787c;
  line-height: 32px;
}

.login-tab .flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.login-tab .go-register {
  margin-top: 24px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  font-size: 14px;
}

.login-tab .go-register .font-color-main {
  color: #4285f4;
  cursor: pointer;
}

/*.view-login .login-content .right .bottom-box {*/
/*  padding-bottom: 12px;*/
/*  margin-top: 35px;*/
/*}*/

.view-login .login-content .right .qr-box {
  overflow: hidden;
  margin: 0 -35px 0;
}

.view-login .login-content .right .qr-box .qr-code {
  position: relative;
  height: 70px;
  width: 70px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background: url(../../../static/login/qr-all.png) no-repeat 0 0/cover;
  transform: rotateY(
    180deg
  );
}

.active {
  border-bottom: 4px solid #4285f4;
}
</style>
